<template>
	<view>
		<!-- 		<p class="property-title">{{propertyInfo.propertyName}}</p>
		<view class="property-input">
			<uni-easyinput type="number" style="flex: 1;" v-model="countChange" placeholder="" />
			<button v-if="countChange != propertyInfo.count" type="primary" @click="updateProperty">修改</button>
		</view>
		<p class="property-title" style="margin-top: 20px;">关联账单</p>
		<billsList :bills-list="propertyBillsList" /> -->
		<uni-card>
			<template v-slot:title class="">
				<u-cell-group>
					<u-cell :title="propertyInfo.propertyName+'：'+propertyInfo.count">
						<u-icon slot="right-icon" name="edit-pen" color="#2979ff" size="20" @click="openDialog">
						</u-icon>
					</u-cell>
				</u-cell-group>
			</template>
			<uni-section title="关联账单" type="line"></uni-section>
			<billsList v-if="propertyBillsList.length" :bills-list="propertyBillsList" />
		</uni-card>
		<u-button v-if="!propertyBillsList.length" style="width: 80rpx;" size="small" type="error" @click="deleteProperty">删除</u-button>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="输入资产" value="" @confirm="updateProperty">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		callCloudFunction
	} from '@/api/requests.js'
	import billsList from '../../components/billsList/billsList.vue'
	export default {
		data() {
			return {
				propertyInfo: {},
				propertyBillsList: [],
				countChange: 0,
			}
		},
		methods: {
			getPropertyAndBillsById(id) {
				callCloudFunction('getProperties', {
						_id: id
					}, res => {
						this.propertyInfo = res.result.data[0]
						this.countChange = res.result.data[0].count
					},
					err => {
						uni.showModal({
							content: `获取资产失败`,
							showCancel: false
						})
					})
				callCloudFunction('getBills', {
						payBy: id
					}, res => {
						this.propertyBillsList = res.result.data
					},
					err => {
						uni.showModal({
							content: `获取资产关联账单失败`,
							showCancel: false
						})
					})
			},
			openDialog() {
				this.$refs.inputDialog.open()
			},
			updateProperty(value) {
				let property = JSON.parse(JSON.stringify(this.propertyInfo))
				property.count = parseFloat(parseFloat(value).toFixed(2))
				callCloudFunction('updateProperty', property, res => {
						uni.showModal({
							content: `资产修改成功`,
							showCancel: false
						})
						this.getPropertyAndBillsById(this.propertyInfo._id)
					},
					err => {
						uni.showModal({
							content: `资产修改失败`,
							showCancel: false
						})
					})
			},
			deleteProperty(){
				callCloudFunction('deleteProperty', {
						_id: this.propertyInfo._id
					}, res => {
						uni.showModal({
							content: `资产删除成功`,
							showCancel: false,
							success: function(res) {
								uni.navigateBack({
									delta: 1
								});
							}
						})
					},
					err => {
						uni.showModal({
							content: `删除资产失败`,
							showCancel: false
						})
					})
			}
		},
		onLoad: function(option) {
			this.getPropertyAndBillsById(option._id)
		}
	}
</script>

<style>
	.property-title {
		margin: 10px 0 15px 10px;
		font-size: 24px;
	}

	.property-input {
		display: flex;
		flex-direction: row;
		padding: 0 15px;
	}

	.property-input>uni-button {
		height: 35px;
		line-height: 35px;
		margin-left: 5px;
	}
</style>
